package com.vcar.LDiceClient.core
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    public class ScaleImage extends Sprite
    {		            
        public var content:Bitmap;
        public var bitmapData:BitmapData;
        public var inner:Rectangle;

        private var sourcebmd:BitmapData;				
        private var wl:Number;
        private var wr:Number;
        private var wm:Number;
        private var ht:Number;
        private var hm:Number;
        private var hb:Number;
        private var rects:Object;
        private var tmbmd:BitmapData;
        private var mlbmd:BitmapData;
        private var mmbmd:BitmapData;
        private var bmbmd:BitmapData;
        private var mrbmd:BitmapData;	
        private var minWidth:Number;
        private var minHeight:Number;	
        public function ScaleImage(bmd:BitmapData,inner:Rectangle)
        {
            sourcebmd=bmd;
            this.inner=inner;

            wl=inner.left;
            wr=sourcebmd.width-inner.right;
            wm=inner.width;
            ht=inner.top;
            hm=inner.height;
            hb=sourcebmd.height-inner.bottom;				
            rects={};
            rects["TL"]=new Rectangle(0,0,wl,ht);
            rects["TM"]=new Rectangle(wl,0,wm,ht);
            rects["TR"]=new Rectangle(inner.right,0,wr,ht);				
            rects["ML"]=new Rectangle(0,ht,wl,hm);
            rects["MM"]=inner;
            rects["MR"]=new Rectangle(inner.right,ht,wr,hm);
            rects["BL"]=new Rectangle(0,inner.bottom,wl,hb);
            rects["BM"]=new Rectangle(inner.left,inner.bottom,wm,hb);
            rects["BR"]=new Rectangle(inner.right,inner.bottom,wr,hb);		

            tmbmd=new BitmapData(wm,ht,true,0x00000000);
            tmbmd.copyPixels(sourcebmd,rects["TM"],new Point(0,0));
            mlbmd=new BitmapData(wl,hm,true,0x00000000);
            mlbmd.copyPixels(sourcebmd,rects["ML"],new Point(0,0));
            mmbmd=new BitmapData(wm,hm,true,0x00000000);
            mmbmd.copyPixels(sourcebmd,rects["MM"],new Point(0,0));
            mrbmd=new BitmapData(wr,hm,true,0x00000000);
            mrbmd.copyPixels(sourcebmd,rects["MR"],new Point(0,0));
            bmbmd=new BitmapData(wm,hb,true,0x00000000);
            bmbmd.copyPixels(sourcebmd,rects["BM"],new Point(0,0));			

            content=new Bitmap();
            addChild(content);

            minWidth=wl+wr;
            minHeight=ht+hb;

            setSize(100,100);
        }

        public function setSize(width:Number,height:Number):void
        {
            if(width<minWidth) width=minWidth;
            if(height<minHeight) height=minHeight;
            if(bitmapData!=null) bitmapData.dispose();

            bitmapData=new BitmapData(width,height,true,0x00000000);
            bitmapData.copyPixels(sourcebmd,rects["TL"],new Point(0,0));				
            bitmapData.copyPixels(sourcebmd,rects["TR"],new Point(width-wr,0));				
            bitmapData.copyPixels(sourcebmd,rects["BL"],new Point(0,height-hb));				
            bitmapData.copyPixels(sourcebmd,rects["BR"],new Point(width-wr,height-hb));							

            var mtr:Matrix=new Matrix();
            mtr.createBox((width-wr-wl)/inner.width,1,0,wl,0);								
            bitmapData.draw(tmbmd,mtr);								
            mtr.createBox(1,(height-ht-hb)/inner.height,0,0,ht);
            bitmapData.draw(mlbmd,mtr);
            mtr.createBox((width-wr-wl)/inner.width,(height-ht-hb)/inner.height,0,wl,ht);				
            bitmapData.draw(mmbmd,mtr);
            mtr.createBox(1,(height-ht-hb)/inner.height,0,width-wr,ht);
            bitmapData.draw(mrbmd,mtr);
            mtr.createBox((width-wr-wl)/inner.width,1,0,wl,height-hb);
            bitmapData.draw(bmbmd,mtr);							
            content.bitmapData=	bitmapData;		
        }
        public function unload():void
        {
            if(bitmapData!=null) bitmapData.dispose();
            bitmapData=null;			
            tmbmd.dispose();			
            mlbmd.dispose();			
            mmbmd.dispose();			
            bmbmd.dispose();			
            mrbmd.dispose();			
            sourcebmd.dispose();			
            sourcebmd=null;
        }
    }
}

